package main

func minimumJumps2(forbidden []int, a int, b int, x int) int {

	visited := make([]bool, 2001+a+b)
	for i := range forbidden {
		visited[forbidden[i]] = true
	}

	queue := [][2]int{{0, 0}}
	res := -1
	for len(queue) > 0 {

		length := len(queue)
		res++
		for i := 0; i < length; i++ {
			c, isBack := queue[i][0], queue[i][1]
			if c == x {
				return res
			}
			if isBack == 0 && c-b > 0 && !visited[c-b] {
				visited[c-b] = true
				queue = append(queue, [2]int{c - b, 1})
			}
			if c+a < len(visited) && !visited[c+a] {
				visited[c+a] = true
				queue = append(queue, [2]int{c + a, 0})
			}
		}
		queue = queue[length:]
	}
	return -1
}
